Ext.ns('AIR2.User');
/***************
 * User creation modal window
 *
 * Opens a modal window allowing the creation of new users.
 *
 * @function AIR2.User.Create
 * @cfg {HTMLElement} originEl (optional) origin element to animate from
 * @cfg {Boolean} redirect (default false) redirect on success
 * @cfg {Function} callback (optional) function passed boolean success
 * @cfg {String} orgUUID (optional) uuid of org to add the user to
 *
 */
AIR2.User.Create = function(cfg) {
    var flds = [{
        xtype: 'air2remotetext',
        fieldLabel: 'Username',
        name: 'user_username',
        remoteUrl: AIR2.HOMEURL + '/user/name.json',
        autoCreate: {tag: 'input', type: 'text', maxlength: '64'}
    },{
        fieldLabel: 'First Name',
        name: 'user_first_name',
        maxLength: 64
    },{
        fieldLabel: 'Last Name',
        name: 'user_last_name',
        maxLength: 64
    },{
        fieldLabel: 'Email',
        name: 'uem_address',
        vtype: 'email',
        maxLength: 255
    }];

    // show org-role picker
    if (cfg.orgUUID) {
        flds.push({
            xtype: 'hidden',
            name: 'org_uuid',
            value: cfg.orgUUID
        });
        flds.push({
            xtype: 'air2combo',
            fieldLabel: 'Role',
            name: 'ar_code',
            width: 80,
            choices: [
                //['X', 'Reporter'],    // disabled for OpenAIR #1550
                ['R', 'Reader'], 
                ['W', 'Writer'], 
                ['M', 'Manager']
            ],
            value: 'R'
        });
    }
    else if (AIR2.USERINFO.type == 'S') {
        // system user can create other system users
        flds.push({
            xtype: 'air2combo',
            fieldLabel: 'User Type',
            name: 'user_type',
            width: 80,
            choices: [['A', 'AIR User'], ['S', 'System User']],
            value: 'A'
        });
    }

    var w = new AIR2.UI.Window({
        title: 'Create User',
        formAutoHeight: true,
        iconCls: 'air2-icon-user',
        closeAction: 'close',
        width: 290,
        height: 300,
        items: {
            xtype: 'form',
            unstyled: true,
            style: 'padding: 10px 10px 0',
            labelWidth: 70,
            defaults: {
                xtype: 'textfield',
                allowBlank: false,
                width: 180,
                msgTarget: 'under'
            },
            items: flds,
            bbar: [{
                xtype: 'air2button',
                air2type: 'SAVE',
                air2size: 'MEDIUM',
                text: 'Save',
                handler: function() {
                    var f = w.get(0).getForm(), el = w.get(0).el;

                    // validate and fire the ajax save
                    if (f.isValid()) {
                        el.mask('Saving');
                        Ext.Ajax.request({
                            url: AIR2.HOMEURL + '/user.json',
                            params: {radix: Ext.encode(f.getFieldValues())},
                            callback: function(opt, success, rsp) {
                                if (success) {
                                    var d = Ext.decode(rsp.responseText);
                                    if (d.success && d.radix.user_uuid) {
                                        if (cfg.callback) cfg.callback(true);
                                        if (cfg.redirect) {
                                            location.href = AIR2.HOMEURL + '/user/' + d.radix.user_uuid;
                                        }
                                        else {
                                            w.close();
                                        }
                                        return;
                                    }
                                }
                                // failed
                                if (cfg.callback) cfg.callback(true, rsp.responseText);
                                el.unmask();
                                alert("TODO: FAILED\n"+rsp.responseText);
                            }
                        });
                    }
                }
            },' ',{
                xtype: 'air2button',
                air2type: 'CANCEL',
                air2size: 'MEDIUM',
                text: 'Cancel',
                handler: function() {w.close();}
            }]
        }
    });
    if (cfg.originEl) w.show(cfg.originEl);
    else w.show();
    return w;
}
